﻿
Imports Capa_de_Acceso_a_Datos

Public Class frmConsultasFarmacias

    Public ConsultaSeleccionadaId As Integer
    Public FarmaciaSeleccionadaId As Integer
    Private fechaDesde As DateTime
    Private fechaHasta As DateTime
    Private idTipoConsulta As Integer
    Private idEstadoConsulta As Integer
    Private inicializando As Boolean

#Region "Inicialización y Carga"

    Private Sub frmConsultasFarmacias_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub frmConsultasFarmacias_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        inicializando = True
        LimpiarFiltrosBusqueda()
        CargarCombosBusqueda()
        CargarGrillaConsultasFarmacias()
        'EditarColumnasGrillaConsultasFarmacias()
        inicializando = False
    End Sub

    Private Sub LimpiarFiltrosBusqueda()
        CargarCombosBusqueda()
        txtFarmacia.Text = ""
        FarmaciaSeleccionadaId = 0
        dtpFechaDesde.Value = New Date(Today.Year, 1, 1)
        dtpFechaHasta.Value = Date.Now
    End Sub

    Private Sub CargarCombosBusqueda()

        'Combo Tipo de Consulta
        cboTipoConsulta.DataSource = TipoConsultaDAO.comboTiposConsultas
        cboTipoConsulta.DisplayMember = "Descripcion"
        cboTipoConsulta.ValueMember = "TipoConsultaId"
        cboTipoConsulta.SelectedValue = 0

        'Combo Estado de Consulta
        cboEstadoConsulta.DataSource = EstadoConsultaDAO.comboEstadoConsulta
        cboEstadoConsulta.DisplayMember = "Descripcion"
        cboEstadoConsulta.ValueMember = "EstadoConsultaId"
        cboEstadoConsulta.SelectedValue = 0

    End Sub

    Public Sub CargarGrillaConsultasFarmacias()
        Try
            dgvConsultas.DataSource = ConsultaFarmaciaDAO.filtroConsultasFarmacias(FarmaciaSeleccionadaId, cboEstadoConsulta.SelectedValue, cboTipoConsulta.SelectedValue, frmMenuAuditoria.empleadoId, dtpFechaDesde.Value, dtpFechaHasta.Value)
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        dgvConsultas.Refresh()
        ColorearGrillaConsultasFarmacias()

    End Sub

    Private Sub ColorearGrillaConsultasFarmacias()
        If dgvConsultas.Rows.Count > 0 Then
            For i As Integer = 0 To dgvConsultas.Rows.Count - 1
                Dim estado As String
                estado = dgvConsultas.Item("EstadoConsultaNombre", i).Value
                If estado = "No Solucionada" Then
                    dgvConsultas.Rows(i).DefaultCellStyle.BackColor = Color.Bisque
                Else
                    dgvConsultas.Rows(i).DefaultCellStyle.BackColor = Color.PaleGreen
                End If
            Next
        End If
    End Sub

#End Region

    Private Sub btnBuscarFarmacia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarFarmacia.Click
        frmSeleccionarFarmacia.frmLlamada = Me.Name
        frmSeleccionarFarmacia.ShowDialog()
        If FarmaciaSeleccionadaId > 0 Then
            CargarGrillaConsultasFarmacias()
        End If
    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click

        ConsultaSeleccionadaId = dgvConsultas.Item("ConsultaFarmaciaId", dgvConsultas.CurrentRow.Index).Value
        ConsultaFarmaciaDAO.eliminarConsultaFarmacia(ConsultaSeleccionadaId)
        CargarGrillaConsultasFarmacias()
        'EditarColumnasGrillaConsultasFarmacias()

    End Sub

    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        frmConsulta.habilitarCarga()
        frmConsulta.ShowDialog()
        CargarGrillaConsultasFarmacias()
        dtpFechaHasta.Value = Now
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        ModificarConsulta()
    End Sub

    Private Sub btnVisualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualizar.Click
        frmConsulta.habilitarVisualización()
        frmConsulta.CargarConsulta(dgvConsultas.SelectedRows(0).Cells("ConsultaFarmaciaId").Value)
        frmConsulta.ShowDialog()
        CargarGrillaConsultasFarmacias()
    End Sub

    Private Sub dgvConsultas_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvConsultas.MouseDoubleClick
        ModificarConsulta()
    End Sub

    Private Sub ModificarConsulta()
        If dgvConsultas.SelectedRows.Count > 0 Then
            frmConsulta.habilitarMoficacion()
            frmConsulta.CargarConsulta(dgvConsultas.SelectedRows(0).Cells("ConsultaFarmaciaId").Value)
            frmConsulta.ShowDialog()
            dtpFechaHasta.Value = Now
        End If

    End Sub

#Region "Eventos de filtro"

    Private Sub dgvConsultas_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvConsultas.SelectionChanged
        If dgvConsultas.SelectedRows.Count > 0 Then
            btnEliminar.Enabled = True
            btnModificar.Enabled = True
            btnVisualizar.Enabled = True
        Else
            btnEliminar.Enabled = False
            btnModificar.Enabled = False
            btnVisualizar.Enabled = False
        End If
    End Sub

    Private Sub cboTipoConsulta_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTipoConsulta.SelectedIndexChanged
        If Not inicializando Then
            CargarGrillaConsultasFarmacias()
        End If
    End Sub

    Private Sub cboEstadoConsulta_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEstadoConsulta.SelectedIndexChanged
        If Not inicializando Then
            CargarGrillaConsultasFarmacias()
        End If
    End Sub

    Private Sub dtpFechaDesde_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpFechaDesde.ValueChanged
        If Not inicializando Then
            CargarGrillaConsultasFarmacias()
        End If
    End Sub

    Private Sub dtpFechaHasta_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpFechaHasta.ValueChanged
        If Not inicializando Then
            CargarGrillaConsultasFarmacias()
        End If
    End Sub

#End Region

End Class